<template>
  <view class="new-or-edit-modal">
    <up-popup v-model:show="show" overlay mode="bottom" touchable maxHeight="'80%'" closeable closeOnClickOverlay
      @close="close" @open="open">
      <view class="modal-content">
        <view class="modal-header">
          <view class="modal-title">{{ title }}</view>
        </view>
        <!-- uview-plus的表单，包含标题，公告内容，单选是否弹窗推送，状态下拉，确认按钮 -->
        <up-form labelPosition="left" :model="formModel" :rules="rules" ref="formRef">
          <up-form-item labelWidth="100" label="标题" prop="title">
            <up-input v-model="formModel.title" placeholder="请输入标题" />
          </up-form-item>
          <up-form-item labelWidth="100" label="公告内容" prop="content">
            <up-input v-model="formModel.content" placeholder="请输入公告内容" />
          </up-form-item>
          <up-form-item labelWidth="100" label="是否弹窗推送" prop="isPopup">
            <up-radio-group v-model="formModel.isPopup">
              <up-radio label="是" name="1"></up-radio>
              <up-radio label="否" name="0"></up-radio>
            </up-radio-group>
          </up-form-item>
          <up-form-item labelWidth="100" label="状态" prop="status">
            <up-select v-model:current="formModel.status" :options="statusOptions" placeholder="请选择状态">
            </up-select>
          </up-form-item>
        </up-form>
      </view>

      <view class="modal-footer">
        <up-button type="primary" @click="submit">确认</up-button>
      </view>
    </up-popup>
  </view>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
const formRef = ref(null);
const props = defineProps({
  type: {
    type: String,
    default: 'add',
  },
  rowData: {
    type: Object,
    default: () => { },
  }
})
defineOptions({ name: "UserNoticeModal" });

// 创建响应式数据  
const show = defineModel<boolean>("show", { default: false });
// watch(show, (val) => {
//   if (val) {
//     handleInitModel();
//     setTimeout(() => {
//       formRef.value.resetFields();
//     }, 0);
//   }
// });

const statusOptions = ref([
  {
    id: '0',
    name: '草稿',
  },
  {
    id: '1',
    name: '已发布',
  },
  {
    id: '2',
    name: '已下架',
  },
])
const formModel = ref(createDefaultModel());
function createDefaultModel() {
  return {
    title: '',
    content: '',
    isPopup: '1',
    status: '1',
  };
}
const rules = reactive({
  title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  content: [{ required: true, message: '请输入公告内容', trigger: 'blur' }],
})
function handleInitModel() {
  formModel.value = createDefaultModel();
  if (!props.rowData) return;
  if (props.type === "edit") {
    Object.assign(formModel.value, props.rowData);
  }
}
const title = computed(() => {
  const titles: any = {
    add: "新增公告",
    edit: "编辑公告",
  };
  return titles[props.type];
});


const submit = async () => {
  console.log(formModel.value);
  await formRef.value.validate();
}

// 定义方法  
function open() {
  show.value = true;
  handleInitModel();
  formRef.value.resetFields();
}

function close() {
  // 关闭逻辑，设置 show 为 false  
  show.value = false;
  console.log('close');
}  
</script>

<style scoped lang="scss">
.new-or-edit-modal {
  .modal-header {
    text-align: center;
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
  }

  .modal-content {
    height: 500px;
    padding: 20rpx;
  }
}
</style>
